<template>
  <div id="system_admin">
    <!--顶部搜索框内容开始-->
    <div class="role_seek">
      <el-input
        class="role_input"
        placeholder="请输入管理员名称"
        v-model="input"
        clearable
        @change="seek"
      ></el-input>
      <el-button type="primary" icon="el-icon-search" size="mini"
        >查找</el-button
      >
      <el-button
        size="mini"
        type="primary"
        icon="el-icon-edit"
        @click="add"
        v-power="'admin:admin.create'"
        >添加</el-button
      >

      <!--修改添加弹出框开始-->
      <el-dialog :title="title" :visible.sync="dialogFormVisible">
        <el-form
          :model="form"
          :rules="rules"
          ref="form"
          class="pop-up"
          status-icon
        >
          <el-form-item
            prop="username"
            label="登录名"
            :label-width="formLabelWidth"
          >
            <el-input
              v-model="form.username"
              autocomplete="off"
              :disabled="disableds"
            ></el-input>
          </el-form-item>

          <el-form-item prop="pwd" label="密码" :label-width="formLabelWidth">
            <el-input
              v-model="form.pwd"
              autocomplete="off"
              type="password"
            ></el-input>
          </el-form-item>

          <el-form-item
            prop="number"
            label="手机号"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.number" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item
            prop="name"
            label="真实姓名"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="授权角色" class="rolelds">
            <el-select v-model="form.rolelds" multiple placeholder="请选择">
              <el-option
                v-for="item in roleId"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancelbutton('form')">取 消</el-button>
          <el-button
            type="primary"
            @click="bool ? adduser('form') : compile('form')"
            >确 定</el-button
          >
        </div>
      </el-dialog>
      <!--修改添加弹出框结束-->
    </div>
    <!--顶部搜索框内容结束-->

    <!--底部列表内容部分-->
    <template>
      <el-table
        border
        :data="tableData"
        style="width: 100%"
        :default-sort="{ prop: 'date', order: 'descending' }"
      >
        <el-table-column
          prop="id"
          label="管理员ID"
          sortable
          align="center"
        ></el-table-column>
        <el-table-column
          prop="username"
          label="管理员名称"
          align="center"
        ></el-table-column>
        <el-table-column label="管理员头像" align="center"></el-table-column>
        <el-table-column label="管理员角色" align="center">
          <template slot-scope="scope">
            <span v-for="item in scope.row.roleIds" :key="item">
              <el-tag>
                {{
                  roleId.find(k => k.id == item)
                    ? roleId.find(k => k.id == item).name
                    : item
                }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              class="button_redact"
              size="mini"
              v-power="'admin:admin:update'"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >

            <el-button
              class="button_remove"
              size="mini"
              type="danger"
              v-power="'admin:admin.delete'"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </template>
    <!--底部列表内容部分结束-->

    <!--底部分页开始-->
    <div class="block">
      <Paging :forms="forms" :gain="gain"></Paging>
    </div>
    <!--底部分页结束-->
  </div>
</template>

<script>
import Admin from "@/api/system/Admin";
import Role from "@/api/system/Role";
import Paging from "../../components/Paging.vue"; //自定义组件

export default {
  data() {
    //验证角色名称是否为空
    var username = (rule, value, callback) => {
      if (value === "") {
        //非空验证
        callback(new Error("角色名称不能为空"));
      } else {
        callback();
      }
    };
    //验证密码是否为空
    var pwd = (rule, value, callback) => {
      let pwds = /^\S{6,18}$/;
      if (value === "") {
        //非空验证
        callback(new Error("密码不能为空"));
      } else if (!pwds.test(value)) {
        callback(new Error("请输入6-18个字母或数字"));
      } else {
        callback();
      }
    };
    //验证手机号
    var number = (rule, value, callback) => {
      let numbers = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
      if (value === "") {
        //非空验证
        callback(new Error("手机号不能为空"));
      } else if (!numbers.test(value)) {
        callback(new Error("手机号格式错误"));
      } else {
        callback();
      }
    };
    //真实姓名
    var name = (rule, value, callback) => {
      let names = /^[\u4e00-\u9fa5]{1,}$/;
      if (value === "") {
        //非空验证
        callback(new Error("真实姓名不能为空"));
      } else if (!names.test(value)) {
        callback(new Error("请输入中文"));
      } else {
        callback();
      }
    };

    return {
      //弹出框
      tableData: [{}], //table里面的数据
      dialogFormVisible: false, //控制弹出框是否显示
      formLabelWidth: "120px", //弹出框宽度
      title: "创建", //弹出框标题
      disableds: false, //输入款是否禁用（登录名）
      bool: true, //布尔值 判断编辑还是修改 为true是创建 否则就是修改
      input: "", //index框输入内容
      //分页
      forms: {
        limit: 20, //limit
        total: 0, //分页——-共几条ds
        pageNo: 1 //页码
      },

      //输入框数值
      form: {
        username: "",
        name: "", //弹出框输入的角色名称
        pwd: "", //弹出框输入的说明
        number: "", //手机号
        rolelds: "", //角色的id
        id: "" //管理员id
      },

      //弹出框验证
      rules: {
        username: [{ required: true, validator: username, trigger: "blur" }], //如果管理员名称验证是正确的就给蓝色
        pwd: [{ required: true, validator: pwd, trigger: "blur" }], //如果说明验证是正确的就给蓝色
        number: [{ required: true, validator: number, trigger: "blur" }], //如果说明验证是正确的就给蓝色
        name: [{ required: true, validator: name, trigger: "blur" }] //如果说明验证是正确的就给蓝色
      },
      //点击当前的下标
      index: "", //当前点击的下标
      id: "", //点击当前的id
      roleId: [{}] //管理员角色id
    };
  },
  components: {
    Paging
  },
  created() {
    this.gain();
    Role.gain(this.forms).then(res => {
      //调用角色管理渲染接口 获取角色id
      this.roleId = res.data.data.items;
    });
  },
  methods: {
    //点击取消按钮
    cancelbutton() {
      this.dialogFormVisible = false;
    },
    //获取列表数据内容
    gain() {
      var _this = this;
      Admin.gain(_this.forms, _this.input).then(res => {
        _this.tableData = res.data.data.items; //将接口列表内容赋值给列表数组
        _this.forms.total = res.data.data.total; //分页————条数
        _this.forms.limit = res.data.data.pageSize; //页码长度
        _this.forms.pageNo = res.data.data.pageNo; //页码
      });
    },
    //点击添加按钮触发
    add() {
      if (this.$refs.form) {
        this.$refs.form.resetFields();
      }
      this.title = "创建"; //将标题改变
      this.bool = true; //bool为true触发添加接口
      this.disableds = false;
      //清空输入框
      this.form.username = ""; //将管理名称清空
      this.form.pwd = ""; //将密码清空
      this.form.name = ""; //将真实姓名清空
      this.form.number = ""; //将手机号清空
      this.form.rolelds = ""; //角色的id
      this.dialogFormVisible = true; //显示弹出框
    },
    //添加新角色接口
    adduser(form) {
      this.$refs[form].validate(valid => {
        if (valid) {
          var _this = this;
          Admin.adduser(_this.form)
            .then(res => {
              if (res.data.errmsg == "成功") {
                this.tableData.splice(this.tableData.length, 1);
                this.tableData.unshift(res.data.data); //实时在列表更新添加
                this.total++; //条数+1
                this.Prosperity.Messages("添加成功角色", "success");
                this.dialogFormVisible = false;
              } else {
                _this.Prosperity.Messages(res.data.errmsg, "error");
              }
            })
            .catch(err => {
              return err;
            });
        } else {
          return false;
        }
      });
    },
    //点击编辑按钮触发
    handleEdit(index, row) {
      //清空输入框下面的提示（输入错误）
      if (this.$refs.form) {
        this.$refs.form.resetFields();
      }

      this.disableds = true;
      this.index = index; //点击时的row下标
      this.form.id = row.id; //获取到本行id并赋值

      //获取到数据在点击进去时赋值入输入框
      this.form.username = row.username; //管理员登录名
      this.form.number = row.phone; //手机号
      this.form.name = row.realname; //管理员真实姓名
      this.form.rolelds = row.roleIds; //管理员角色id
      this.bool = false; //改变bool 触发编辑接口
      this.title = "编辑"; //改变弹出框标题
      this.dialogFormVisible = true; //显示弹出框
    },

    //编辑角色接口
    compile(form) {
      this.$refs[form].validate(valid => {
        if (valid) {
          var _this = this;
          Admin.compile(_this.form).then(res => {
            //弹框  如果用户名或者密码等于空的话就弹出
            if (res.data.errmsg == "成功") {
              //使用数据再次更新页面
              _this.tableData[_this.index].phone = this.form.number; //手机号
              _this.tableData[_this.index].realname = this.form.name; //管理员真实姓名
              _this.tableData[_this.index].roleIds = this.form.rolelds; //角色的id
              //限制弹出框数量
              _this.Prosperity.Messages("编辑角色成功", "success");
              _this.dialogFormVisible = false;
            } else {
              _this.Prosperity.Messages(res.data.errmsg, "error");
            }
          });
        } else {
          return false;
        }
      });
    },
    //搜索信息
    seek() {
      this.gain();
    },
    handleDelete(row) {
      this.id = row.id; //当前删除的id
      this.$confirm(
        "此操作将永久删除该角色---" + row.username + "---, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(() => {
          //确定删除的话
          this.cancel();
        })
        .catch(() => {
          this.Prosperity.Messages("已取消删除");
        });
    },
    //删除管理员内容接口
    cancel() {
      var _this = this;
      Admin.cancel(_this.id)
        .then(res => {
          if (res.data.errmsg == "成功") {
            //如果res返回的值为成功
            _this.gain(); //重新渲染
            this.Prosperity.Messages("删除角色成功", "success");
          } else {
            _this.Prosperity.Messages(res.data.errmsg, "error");
          }
        })
        .catch(err => {
          return err;
        });
    }
  }
};
</script>

<style scoped="scoped">
/*最大父盒子开始*/
#system_admin {
  padding: 20px;
}
/*最大父盒子结束*/

/*顶部index框开始*/
.role_seek > .el-input {
  width: 200px !important;
  height: 40px !important;
}
.role_seek > .el-input > .el-input__inner {
  font-size: inherit;
  width: 50% !important;
  height: 100% !important;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
}
.role_input .el-input__suffix {
  left: 170px !important;
}
/*顶部index框结束*/

/*顶部按钮开始*/
.role_seek > .el-button--primary {
  line-height: 1px;
  font-size: 14px;
  border: 1px solid #409eff !important;
  background: #409eff !important;
  color: #fff;
  vertical-align: middle;
  padding: 12px 20px;
  margin-left: 5px;
}
/*顶部按钮结束*/

/*底部列表内容*/
.el-table {
  margin-top: 23px;
}
.cell {
  text-align: center;
  font-size: 12px;
  padding: 0px 10px;
}
.el-table__row > td {
  padding: 8px 0px;
}
.button_redact {
  background: #409eff !important;
  color: #fff !important;
  border: 1px solid #409eff;
}
/*编辑内容按钮*/
.button_redact:hover {
  background: #66b1ff !important;
  border: 1px solid #66b1ff;
}
.button_remove {
  background: #f56c6c !important;
  color: #fff !important;
  border: #f56c6c 1px solid;
}
.button_remove:hover {
  background: #f78989 !important;
  border: 1px solid #f78989;
}
.el-table--scrollable-x .el-table__body-wrapper {
  overflow-x: hidden;
}
/*底部列表内容*/

/*底部分页*/
.block {
  margin: 55px 0px;
}

/*底部分页结束*/

/*添加弹出框*/
.el-form-item__label {
  text-align: left;
  margin-left: 50px;
  width: 85px !important;
}
.el-input__inner {
  text-indent: 5px;
}
.pop-up
  > .el-form-item
  > .el-form-item__content
  > .el-input
  > .el-input__inner {
  width: 268px !important;
  height: 38px;
  padding: 15px 0px;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
}

.pop-up > .el-form-item > .el-form-item__content > .el-form-item__error {
  left: 0px !important;
}
/*添加弹出框结束*/
.el-select .el-input__inner {
  border: 1px solid #dcdfe6;
}
.el-select__caret {
  position: absolute;
  right: 58px !important;
}
.rolelds .el-input__suffix > i {
  position: absolute;
  right: 24px;
}
</style>
